// pages/shopCar/shopCar.js
// pages/chartere_bus6/reserve6-4/reserve6-4.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoppingList: [],
    money: 0,
    all: '',
    i:0,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var userInfo = util.getKey("userInfo");

    if (userInfo == false || userInfo == '') {
      app.onLaunch();
    }
  },
  getShoppingList: function() {
    var that = this;
    var userInfo = util.getKey("userInfo");

    httpUtil.postHttp("/car/get/list?userId=" + userInfo.id, {}, function(res) {
     
      that.setData({
        shoppingList: res
      })
    })
  },
  checkboxChange: function(e) {
    var that = this;
    var index = e.target.dataset.index;
    console.log(index);
    var list = that.data.shoppingList;

    if (index != null) {
      var checked = that.data.shoppingList[index].checked;

      list[index].checked = (checked != null && checked != '') ? '' : "checked";
      console.log(list);
        that.setData({
          shoppingList: list
        })
    } else {
        var checked = (that.data.all == "checked" ? "" : "checked");
        for (var i = 0; i < list.length; i++) {
          list[i].checked = checked;
        console.log(list);
     
      }
      that.setData({
        shoppingList: list,
        all: that.data.all == "checked" ? "" : "checked"
      })
    } 
    that.setMoney();

  },setMoney:function(){
    var that = this;
    var list = that.data.shoppingList;
    var money=0;
    var total=0;
    console.log("进入");
    for (var i = 0; i < list.length; i++) {
      if (list[i].checked =='checked'){
        money = money + (parseInt(list[i].goodsCar.num) * parseFloat(list[i].money));
        total = total+1;
      } 
    }
    that.setData({
      money: parseFloat(money.toFixed(2)),
      total: total
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that=this;
    this.setData({
      money: 0,
      total: 0,
      all: '',
      shoppingList:[]
    })
    this.getShoppingList();
   
     

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  sureMoney: function() {
    var that=this;
    that.data.i=1;
    if(that.data.total==0){
      wx.showToast({
        title: '请选择结算商品',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return ;
    }
    wx.navigateTo({
      url: '../ordercar/ordercar'
    })
  },
 
  showDetail: function(e) {
    var id = e.target.dataset.id;

    wx.navigateTo({
      url: '../proInfo/proInfo?id='+id
    })
  },

    delCar: function(e) {
    var that=this;
    var id = e.target.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要删除吗？',
            success: function (sm) {
                if (sm.confirm) {
                    httpUtil.postHttp("/car/del?id=" + id, {}, function(res) {
                        var shoppingList=that.data.shoppingList;
                        var list=[];
                        for(var i=0;i<shoppingList.length;i++){
                            if(id!=shoppingList[i].goodsCar.id){
                                list.push(shoppingList[i]);
                            }
                        }
                        that.setData({
                            shoppingList: list
                        })
                        that.setMoney();
                    })
                } else if (sm.cancel) {
                    console.log('用户点击取消')
                }
            }
        })



  }
})